package com.atguigu.ggkt.wechat.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.ggkt.model.wechat.Menu;
import com.atguigu.ggkt.vo.wechat.MenuVo;
import com.atguigu.ggkt.wechat.mapper.MenuMapper;
import com.atguigu.ggkt.wechat.service.MenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.SneakyThrows;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单明细 订单明细 服务实现类
 * </p>
 *
 * @author zfj
 * @since 2022-07-10
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

	@Autowired
	private WxMpService wxMpService;

	@Override
	public List<MenuVo> findMenuInfo() {
		//创建list集合用户最终封装数据
		List<MenuVo> list = new ArrayList<>();
		//查询所以菜单数据
		List<Menu> menuList = baseMapper.selectList(null);
		//从所有菜单中获取所有一级菜单
		List<Menu> oneMenuList = menuList.stream().filter(menu -> menu.getParentId().longValue() == 0).collect(Collectors.toList());
		//封装一级菜单数据，封装到最终数据list集合
		for(Menu oneMenu : oneMenuList) {
			MenuVo oneMenuVo = new MenuVo();
			BeanUtils.copyProperties(oneMenu, oneMenuVo);

			//封装二级数据
			List<Menu> twoMenuList = menuList.stream()
					.filter(menu -> menu.getParentId().longValue() == oneMenu.getId())
					.sorted(Comparator.comparing(Menu::getSort))
					.collect(Collectors.toList());
			List<MenuVo> children = new ArrayList<>();

			for(Menu twoMenu : twoMenuList) {
				MenuVo twoMenuVo = new MenuVo();
				BeanUtils.copyProperties(twoMenu, twoMenuVo);
				children.add(twoMenuVo);
			}
			//将二级菜单数据加到一级菜单中
			oneMenuVo.setChildren(children);
			list.add(oneMenuVo);
		}
		return list;
	}

	@Override
	public List<Menu> findMenuOneInfo() {
		QueryWrapper<Menu> wrapper = new QueryWrapper<>();
		wrapper.eq("parent_id",0);
		List<Menu> list = baseMapper.selectList(wrapper);
		return list;
	}

	//同步公众号菜单
	/**
	 * 说明：
	 * 自定义菜单最多包括3个一级菜单，每个一级菜单最多包含5个二级菜单。
	 * 一级菜单最多4个汉字，二级菜单最多8个汉字，多出来的部分将会以“...”代替。
	 * 创建自定义菜单后，菜单的刷新策略是，在用户进入公众号会话页或公众号profile页时，如果发现上一次拉取菜单的请求在5分钟以前，就会拉取一下菜单，如果菜单有更新，就会刷新客户端的菜单。测试时可以尝试取消关注公众账号后再次关注，则可以看到创建后的效果。
	 */
//	@SneakyThrows
	@Override
	public void syncMenu() throws WxErrorException {
		List<MenuVo> menuVoList = this.findMenuInfo();
		//菜单
		JSONArray buttonList = new JSONArray();
		for(MenuVo oneMenuVo : menuVoList) {
			JSONObject one = new JSONObject();
			one.put("name", oneMenuVo.getName());
			JSONArray subButton = new JSONArray();
			for (MenuVo twoMenuVo : oneMenuVo.getChildren()) {
				JSONObject view = new JSONObject();
				view.put("type", twoMenuVo.getType());
				if (twoMenuVo.getType().equals("view")) {
					view.put("name", twoMenuVo.getName());
					view.put("url", "http://ggkt2.vipgz1.91tunnel.com/#"
							+ twoMenuVo.getUrl());
				} else {
					view.put("name", twoMenuVo.getName());
					view.put("key", twoMenuVo.getMeunKey());
				}
				subButton.add(view);
			}
			one.put("sub_button", subButton);
			buttonList.add(one);
		}
		//菜单
		JSONObject button = new JSONObject();
		button.put("button", buttonList);

		String menuId = wxMpService.getMenuService().menuCreate(button.toJSONString());

		System.out.println("menuId"+menuId);
	}

	//删除公众号菜单
//	@SneakyThrows
	@Override
	public void removeMenu() throws WxErrorException {
		wxMpService.getMenuService().menuDelete();
	}
}
